<?php

Raise::load('core.text.RaiseText');

/**
 * TestCoreRaiseText class
 * Testing unit of 'phpraise/core/RaiseText.php'
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseText extends RaiseTest {

    public static function testClass(){
        self::assertClassExists('RaiseText');
        self::assertClassExtends('RaiseText', 'RaiseStaticClass');
        self::assertConstantDefined('RaiseText::NEWLINE_WIN');
        self::assertConstantDefined('RaiseText::NEWLINE_MAC');
        self::assertConstantDefined('RaiseText::NEWLINE_NIX');

        self::assertClassMethod('RaiseText', 'neutralizeNewlines');
        self::assertClassMethod('RaiseText', 'truncate');
        self::assertClassMethod('RaiseText', 'highlight');
        self::assertClassMethod('RaiseText', 'stripTags');
        self::assertClassMethod('RaiseText', 'linkify');
        self::assertClassMethod('RaiseText', 'listing');
    }

    public static function testNeutralizeNewLines(){
        $s = "testing\rcool\nyay";
        $s = RaiseText::neutralizeNewlines($s, RaiseText::NEWLINE_WIN);
        self::assertStrictEqual($s, 'testing' . RaiseText::NEWLINE_WIN . 'cool' . RaiseText::NEWLINE_WIN . 'yay');
        $s = "testing\r\ncool\n\ryay";
        $s = RaiseText::neutralizeNewlines($s, RaiseText::NEWLINE_WIN);
        self::assertStrictEqual($s, 'testing' . RaiseText::NEWLINE_WIN . 'cool' . RaiseText::NEWLINE_WIN . RaiseText::NEWLINE_WIN . 'yay');
        $s = "testing\r\ncool\r\ryay";
        $s = RaiseText::neutralizeNewlines($s, RaiseText::NEWLINE_NIX);
        self::assertStrictEqual($s, 'testing' . RaiseText::NEWLINE_NIX . 'cool' . RaiseText::NEWLINE_NIX . RaiseText::NEWLINE_NIX. 'yay');
        $s = "testing\r\ncool\n\nyay";
        $s = RaiseText::neutralizeNewlines($s, RaiseText::NEWLINE_MAC);
        self::assertStrictEqual($s, 'testing' . RaiseText::NEWLINE_MAC . 'cool' . RaiseText::NEWLINE_MAC . RaiseText::NEWLINE_MAC . 'yay');
    }

    public static function testTruncate(){
        $s = 'This is a very long paragraph consisting of <b>several factors</b> listed on another lol-page (next page). Make sure that the quick brown fox jumps over the big lazy dog.';
        self::assertStrictEqual(RaiseText::truncate($s, 20), 'This is a very long...');
        self::assertStrictEqual(RaiseText::truncate($s, 83), 'This is a very long paragraph consisting of <b>several factors</b> listed on...');
        self::assertStrictEqual(RaiseText::truncate($s, 47), 'This is a very long paragraph consisting of...');
        self::assertStrictEqual(RaiseText::truncate($s, 46), 'This is a very long paragraph consisting of...');
        self::assertStrictEqual(RaiseText::truncate($s, 47, '###'), 'This is a very long paragraph consisting of###');
        self::assertStrictEqual(RaiseText::truncate($s, 46, '###'), 'This is a very long paragraph consisting of###');

        $s = 'This is awesome short.';
        self::assertStrictEqual(RaiseText::truncate($s, 200), $s);
        self::assertStrictEqual(RaiseText::truncate($s, 200, '%%%'), $s);
    }

    public static function testHighlight(){
        $s = 'This is a very long paragraph consisting of several factors listed on another lol-page (next page). Make sure that the quick brown fox jumps over the big lazy dog.';
        self::assertStrictEqual(RaiseText::highlight($s, 'the'), 'This is a very long paragraph consisting of several factors listed on ano<b>the</b>r lol-page (next page). Make sure that <b>the</b> quick brown fox jumps over <b>the</b> big lazy dog.');
        self::assertStrictEqual(RaiseText::highlight($s, 'the', '<span>$1</span>'), 'This is a very long paragraph consisting of several factors listed on ano<span>the</span>r lol-page (next page). Make sure that <span>the</span> quick brown fox jumps over <span>the</span> big lazy dog.');
    }

    public static function testStripTags(){
        $s = '<b>bold <a href="http://www.example.com/">text</a></b>';
        self::assertStrictEqual(RaiseText::stripTags($s), 'bold text');
    }

    public static function testLinkify(){
        $s = 'something is happening right now; Go to http://www.example.com/ or www.cool.com/index. Email me if you want too: mauris@example.com I am bored and I have no idea why.';
        self::assertStrictEqual(RaiseText::linkify($s), 'something is happening right now; Go to <a href="http://www.example.com/">http://www.example.com/</a> or <a href="http://www.cool.com/index">www.cool.com/index</a>. Email me if you want too: <a href="mailto:mauris@example.com">mauris@example.com</a> I am bored and I have no idea why.');
        self::assertStrictEqual(RaiseText::linkify($s, null, false), 'something is happening right now; Go to http://www.example.com/ or www.cool.com/index. Email me if you want too: <a href="mailto:mauris@example.com">mauris@example.com</a> I am bored and I have no idea why.');
        self::assertStrictEqual(RaiseText::linkify($s, null, true, false), 'something is happening right now; Go to <a href="http://www.example.com/">http://www.example.com/</a> or <a href="http://www.cool.com/index">www.cool.com/index</a>. Email me if you want too: mauris@example.com I am bored and I have no idea why.');

        $tag = new RaiseHtmlTag('a', 'TestText', array('title' => 'Test'));
        self::assertStrictEqual(RaiseText::linkify($s, $tag), 'something is happening right now; Go to <a title="Test" href="http://www.example.com/">TestText</a> or <a title="Test" href="http://www.cool.com/index">TestText</a>. Email me if you want too: <a title="Test" href="mailto:mauris@example.com">TestText</a> I am bored and I have no idea why.');
        self::assertStrictEqual(RaiseText::linkify($s, $tag, false), 'something is happening right now; Go to http://www.example.com/ or www.cool.com/index. Email me if you want too: <a title="Test" href="mailto:mauris@example.com">TestText</a> I am bored and I have no idea why.');
        self::assertStrictEqual(RaiseText::linkify($s, $tag, true, false), 'something is happening right now; Go to <a title="Test" href="http://www.example.com/">TestText</a> or <a title="Test" href="http://www.cool.com/index">TestText</a>. Email me if you want too: mauris@example.com I am bored and I have no idea why.');
    }

    public static function testListing(){
        $a = array('Apple', 'pear', 'orange');
        self::assertStrictEqual(RaiseText::listing($a), 'Apple, pear and orange');
        self::assertStrictEqual(RaiseText::listing($a, 'or'), 'Apple, pear or orange');
        self::assertStrictEqual(RaiseText::listing($a, 'or', ','), 'Apple,pear or orange');
        self::assertStrictEqual(RaiseText::listing($a, 'and', '+'), 'Apple+pear and orange');
    }


}
